﻿using NHibernate;
using NLog;
using Susha.Areas.Admin.Models.Requests;
using Susha.Models.Pages;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Susha.Models.Helpers
{
    public static class GuestRepository
    {
        private static Logger _logger = LogManager.GetCurrentClassLogger();

        public static void Save(GuestComment comment)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    comment.CommentDate = DateTime.Now;
                    session.Save(comment);
                    transaction.Commit();
                }
            }
        }

        /*public static GuestPage PerformGuestPage(Int32 page = 0)
        {
            GuestPage guestPage = new GuestPage();
            Int32 rowCount = 0;
            using (ISession session = NHibernateHelper.OpenSession())
            {
                rowCount = session.QueryOver<GuestComment>().RowCount();
                if (COMMENTS_PER_PAGE * (page - 1) > rowCount)
                {
                    page = 0;
                }

                guestPage.Comments = session.QueryOver<GuestComment>().OrderBy(x => x.Id).Desc.Take(COMMENTS_PER_PAGE).Skip(COMMENTS_PER_PAGE * page).List<GuestComment>();
            }

            guestPage.PageNomber = page;

            guestPage.Next = COMMENTS_PER_PAGE * (page + 1) < rowCount && rowCount != 0;
            guestPage.Prev = page > 0;
            return guestPage;
        }
            */

        public static IList<GuestComment> GetComments(Int32 pageNumber)
        {
            IList<GuestComment> result;
            using (ISession session = NHibernateHelper.OpenSession())
            {
                result = session
                    .QueryOver<GuestComment>()
                    .OrderBy(x => x.Id)
                    .Desc
                    .Take(CommonTools.COMMENTS_PER_GUEST_PAGE)
                    .Skip(CommonTools.COMMENTS_PER_GUEST_PAGE * pageNumber)
                    .List<GuestComment>();
            }
            return result
                ?? new List<GuestComment>();
        }

        public static Int32 GetGuestCommentCount()
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                return session.QueryOver<GuestComment>().RowCount();
            }
        }

        public static String SetAdminComment(AdminComment comment)
        {
            String result = "Error";
            using (ISession session = NHibernateHelper.OpenSession())
            {
                GuestComment guestComment = session.QueryOver<GuestComment>().Where(x => x.Id == comment.CommentId).SingleOrDefault();
                if (guestComment != null)
                {
                    guestComment.AdminComment = comment.AdminCommentValue;
                    using (ITransaction transaction = session.BeginTransaction())
                    {
                        session.Update(guestComment);
                        transaction.Commit();

                        result = "Ok";
                    }
                }
                else
                {
                    _logger.Error(String.Format("GuestRepository:SetAdminComment({0}): null result", comment.CommentId));
                }
            }
            return result;
        }
    }
}